import { Component, OnInit, ViewChild, ElementRef, Renderer2, ViewChildren, QueryList, AfterViewInit, TemplateRef, ViewContainerRef } from '@angular/core';
import { UserService } from '../user.service';
import { LoginUser } from './login-user';
import { Router } from '@angular/router';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit, AfterViewInit {

  list = ['a', 'b', 'c', 'd'];
  formInfo: LoginUser;
  @ViewChild('labelTel', { static: true }) labelTel: ElementRef;
  @ViewChildren('item') itemList: QueryList<ElementRef>;

  @ViewChild('tpl', { static: true })
  tplRef: TemplateRef<any>;

  constructor(private us: UserService, private vcRef: ViewContainerRef, private router: Router, private rd2: Renderer2) {
    this.formInfo = new LoginUser();
  }

  ngOnInit() {

  }
  ngAfterViewInit(): void {
    console.log(this.itemList);
    this.itemList.forEach(item => {
      this.rd2.setStyle(item.nativeElement, 'color', 'red');
    });

    this.vcRef.createEmbeddedView(this.tplRef);
  }

  onLogin() {
    this.us.login(this.formInfo).subscribe(
      (result) => {
        if (result) {
          this.router.navigate(['/admin']);
        } else {
          alert('登陆失败');
        }
      },
      (error) => {
        console.log('error', JSON.stringify(error));
      }
    );
  }

}
